package com.guigu.wenzhiyi.controller;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.guigu.tankai.mapper.MemberMapper;
import com.guigu.tankai.pojo.Member;
import com.guigu.wenzhiyi.pojo.DuiXiangs;
import com.guigu.wenzhiyi.pojo.Mentalityresult;
import com.guigu.wenzhiyi.pojo.Mentalitytest;
import com.guigu.wenzhiyi.service.MentalityresultService;
import com.guigu.wenzhiyi.service.MentalitytestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.List;

@RestController
@RequestMapping("Mentalityresult")
public class MentalityresultController {

    @Autowired
    MentalityresultService mentalityresultService;
    @Autowired
    MentalitytestService mentalitytestService;
    @Autowired
    MemberMapper memberMapper;

    // 判断体质的方法
    public static int TiZhi(List<Mentalitytest> bodytests) {
        int zhi = 0;  // 判断条件使用
        int max = 0; // 总条数
        int zong = 0; // 选中的总分数
        DecimalFormat df = new DecimalFormat("#.00");

        for (Mentalitytest bodytest : bodytests) {
            // 一条的分数
            zong += Integer.parseInt(df.format((double) bodytest.getOption() / 5).replace(".", ""));
            max += 1;
        }
        zhi += Integer.parseInt(df.format((double) zong / (max * 100)).replace(".", ""));
        if (zhi >= 60) {
            return 0;
        } else {
            return 1;
        }
    }
    public static List<Mentalitytest> TiMu(List<Mentalitytest> bodytests, int type, int Bid) {
        for (Mentalitytest bodytest : bodytests) {
            bodytest.setType(type);
            bodytest.setMentalityresultid(Bid);
        }
        return bodytests;
    }

    @RequestMapping("/page")
    public PageInfo<Mentalityresult> page(Integer num, Integer size, String name) {
        return mentalityresultService.page(num, size, (name.equals("") ? null : name));
    }

    @RequestMapping("/add")
    public int add(@RequestBody DuiXiangs duiXiangs) {
        Member m = new Gson().fromJson(duiXiangs.getMember(), Member.class);
        List<Mentalitytest> ocd = new Gson().fromJson(duiXiangs.getYangxus(), new TypeToken<List<Mentalitytest>>() {
        }.getType());
        List<Mentalitytest> anxiety = new Gson().fromJson(duiXiangs.getYinxus(), new TypeToken<List<Mentalitytest>>() {
        }.getType());
        List<Mentalitytest> terror = new Gson().fromJson(duiXiangs.getQixus(), new TypeToken<List<Mentalitytest>>() {
        }.getType());

        Mentalityresult mentalityresult = new Mentalityresult();
        mentalityresult.setMemberid(m.getId());
        // 阳虚体质的结果
        mentalityresult.setOcd(TiZhi(ocd));
        // 阴虚体质的判断
        mentalityresult.setAnxiety(TiZhi(anxiety));
        // 阴虚体质的判断
        mentalityresult.setTerror(TiZhi(terror));

        // 添加 体质类型表
        int id = mentalityresultService.add(mentalityresult);
        //添加 体质测试 （题目）
        mentalitytestService.insert(TiMu(ocd,1,id));
        mentalitytestService.insert(TiMu(anxiety,2,id));
        mentalitytestService.insert(TiMu(terror,3,id));

        m.setCheckitemzt(1);
        memberMapper.zt(m);
        return id;
    }

    @RequestMapping("/update")
    public int update(@RequestBody DuiXiangs duiXiangs) {
        Mentalityresult m = new Gson().fromJson(duiXiangs.getMember(), Mentalityresult.class);
        List<Mentalitytest> ocd = new Gson().fromJson(duiXiangs.getYangxus(), new TypeToken<List<Mentalitytest>>() {
        }.getType());
        List<Mentalitytest> anxiety = new Gson().fromJson(duiXiangs.getYinxus(), new TypeToken<List<Mentalitytest>>() {
        }.getType());
        List<Mentalitytest> terror = new Gson().fromJson(duiXiangs.getQixus(), new TypeToken<List<Mentalitytest>>() {
        }.getType());

        // 阳虚体质的结果
        m.setOcd(TiZhi(ocd));
        // 阴虚体质的判断
        m.setAnxiety(TiZhi(anxiety));
        // 阴虚体质的判断
        m.setTerror(TiZhi(terror));

        // 添加 体质类型表
        int id = mentalityresultService.update(m);
        //添加 体质测试 （题目）
        mentalitytestService.updates(ocd);
        mentalitytestService.updates(anxiety);
        mentalitytestService.updates(terror);

        return id;
    }

    @RequestMapping("/delete")
    public int delete(Integer id){
        int num = mentalitytestService.delete(id);
        mentalityresultService.delete(id);

        Member member = new Member();
        member.setId(id);
        member.setCheckitemzt(0);
        memberMapper.zt(member);
        return num;
    }




}
